package com.example.mongodb_learn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试Mongodb事务
 */
@Service
public class TransactionExample {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Transactional(rollbackFor = Exception.class)
    public void transactionTest(){
        Student s = new Student();
        s.setAge(6);
        s.setName("测试事务回滚");

        Student insert = mongoTemplate.insert(s);
        // 抛出异常，观察数据是否进行回滚
        int k =1/0;
    }
}
